package com.lindroy.androidplayer.ui.scan

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import cn.bingoogolapple.qrcode.core.QRCodeView
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseActivity
import com.lindroy.androidplayer.extensions.no
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.ui.webview.ContentActivity
import com.lindroy.lib_utils.extensions.setFullScreen
import com.lindroy.lib_utils.extensions.statusbar.setTransParentStatusBar
import com.lindroy.lib_utils.extensions.statusbar.statusBarHeight
import com.lindroy.lib_utils.statics.VibratorUtil
import com.lindroy.lib_utils.statics.permission.PermissionX
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_scan.*

/**
 * @author Lin
 * @date 2021/5/3
 * @function 扫一扫
 * @Description
 */
class ScanActivity(override val layoutId: Int=R.layout.activity_scan) : BaseActivity() {

    override fun initView() {
        initToolBar("",toolBarColor = R.color.transparent)
        setTransParentStatusBar()
        toolBar.setPadding(0, statusBarHeight, 0, 0)
        PermissionX.request(this,Manifest.permission.CAMERA){allGranted, deniedList ->
            allGranted.yes {
                scanView.startCamera()
                scanView.startSpotAndShowRect()
            }.no {
                finish()
            }
        }
    }
    override fun initData() {
    }

    override fun initListener() {
        scanView.setDelegate(object: QRCodeView.Delegate{
            override fun onScanQRCodeSuccess(result: String?) {
                VibratorUtil.vibrate(0,250)
//                ContentActivity.start()
            }

            override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {
            }

            override fun onScanQRCodeOpenCameraError() {
                Logger.e("扫描出错")
            }

        })
    }

    override fun initClick() {
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onDestroy() {
        scanView.onDestroy()
        super.onDestroy()
    }

    override fun onStop() {
        scanView.stopCamera()
        super.onStop()
    }
}